/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import static gui.GUIUtils.initTextHeaderJlabel;
import static gui.GUIUtils.listToString;

import gui.utilities.Rater;

import java.awt.Dimension;
import java.util.List;
import java.util.concurrent.Future;

import javax.swing.JOptionPane;

import core.exceptions.DBAccessException;
import core.objects.Book;
import core.objects.BookComment;
import core.objects.User;
import core.threads.ThreadCallable;
import core.threads.ThreadPool;
import db.DBAccessEngine;
import db.IDBAccess;
import db.MySQLFormatDate;

public class JPanelBookInfo extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    public static final int OPEN_PANEL_HEIGHT = 400;
    public static final int CLOSE_PANEL_HEIGHT = 90;
    private static final String USER_COMMENT_INITIAL_VALUE = "What did you think of this book?";
    private Rater myUserRater;
    private Rater usersRater;
    private Book book;

    /**
     * Creates new form BookPanel
     */
    public JPanelBookInfo(Book book) {
        this.book = book;
        initComponents();
        initHeaderComponents();
        setMaximumHeight(CLOSE_PANEL_HEIGHT);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelBookInfoHeaderWithPic = new javax.swing.JPanel();
        jPanelBookHeader = new javax.swing.JPanel();
        jLabelAuthor = new javax.swing.JLabel();
        jLabelPublisher = new javax.swing.JLabel();
        jLabelCategory = new javax.swing.JLabel();
        jLabelPublicationYear = new javax.swing.JLabel();
        jLabelCategoryPageCount = new javax.swing.JLabel();
        jLabelISBN = new javax.swing.JLabel();
        jPanelBookTitle = new javax.swing.JPanel();
        jLabelBookName = new javax.swing.JLabel();
        jLabelWiki = new javax.swing.JLabel();
        jLabelAddToMyBooks = new javax.swing.JLabel();
        jLabelEnlarge = new javax.swing.JLabel();
        jLabelIWantIt = new javax.swing.JLabel();
        jPanelRating = new javax.swing.JPanel();
        jLabelImage = new javax.swing.JLabel();
        jTabbedPane = new javax.swing.JTabbedPane();
        jPanelGoogle = new javax.swing.JPanel();
        jScrollPane = new javax.swing.JScrollPane();
        jTextAreaGoogleDescription = new javax.swing.JTextArea();
        jPanelGoogleRating = new javax.swing.JPanel();
        jPanelUserComments = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaUserComment = new javax.swing.JTextArea();
        jPanelUserRater = new javax.swing.JPanel();
        jLabelSubmitButton = new javax.swing.JLabel();
        jScrollPaneUsersComments = new javax.swing.JScrollPane();
        jPanelComments = new javax.swing.JPanel();

        setAlignmentX(0.0F);
        setAlignmentY(0.0F);
        setOpaque(false);

        jPanelBookInfoHeaderWithPic.setOpaque(false);

        jPanelBookHeader.setOpaque(false);

        jLabelAuthor.setText("Author:");

        jLabelPublisher.setText("Publisher:");

        jLabelCategory.setText("Category: ");

        jLabelPublicationYear.setText("Publication Year: ");

        jLabelCategoryPageCount.setText("Page Count: ");

        jLabelISBN.setText("ISBN: ");

        jPanelBookTitle.setOpaque(false);

        jLabelBookName.setText("Book Name");

        jLabelWiki.setText("[Wiki]");

        jLabelAddToMyBooks.setText("[Add to My-Books]");
        jLabelAddToMyBooks.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelAddToMyBooksMouseClicked(evt);
            }
            @Override
			public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabelAddToMyBooksMouseEntered(evt);
            }
        });

        jLabelEnlarge.setText("[+]");
        jLabelEnlarge.setToolTipText("Sounds Interesting...");
        jLabelEnlarge.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelEnlargeMouseClicked(evt);
            }
        });

        jLabelIWantIt.setText("[Find Owners]");
        jLabelIWantIt.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelIWantItMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanelBookTitleLayout = new javax.swing.GroupLayout(jPanelBookTitle);
        jPanelBookTitle.setLayout(jPanelBookTitleLayout);
        jPanelBookTitleLayout.setHorizontalGroup(
            jPanelBookTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBookTitleLayout.createSequentialGroup()
                .addComponent(jLabelEnlarge)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelBookName, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                .addGap(80, 80, 80)
                .addComponent(jLabelWiki)
                .addGap(10, 10, 10)
                .addComponent(jLabelAddToMyBooks)
                .addGap(10, 10, 10)
                .addComponent(jLabelIWantIt)
                .addContainerGap())
        );
        jPanelBookTitleLayout.setVerticalGroup(
            jPanelBookTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBookTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabelBookName)
                .addComponent(jLabelWiki)
                .addComponent(jLabelAddToMyBooks, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabelEnlarge)
                .addComponent(jLabelIWantIt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelRating.setOpaque(false);
        jPanelRating.setLayout(new java.awt.GridLayout(1, 1));

        javax.swing.GroupLayout jPanelBookHeaderLayout = new javax.swing.GroupLayout(jPanelBookHeader);
        jPanelBookHeader.setLayout(jPanelBookHeaderLayout);
        jPanelBookHeaderLayout.setHorizontalGroup(
            jPanelBookHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBookHeaderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelBookHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelBookHeaderLayout.createSequentialGroup()
                        .addComponent(jPanelBookTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(58, 58, 58)
                        .addComponent(jPanelRating, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelBookHeaderLayout.createSequentialGroup()
                        .addGroup(jPanelBookHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelAuthor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelPublisher, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelBookHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelCategoryPageCount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelISBN, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelBookHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelCategory, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelPublicationYear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanelBookHeaderLayout.setVerticalGroup(
            jPanelBookHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBookHeaderLayout.createSequentialGroup()
                .addGroup(jPanelBookHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelBookHeaderLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelBookTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanelRating, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelBookHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelAuthor)
                    .addComponent(jLabelCategory)
                    .addComponent(jLabelCategoryPageCount))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelBookHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelPublisher)
                    .addComponent(jLabelPublicationYear)
                    .addComponent(jLabelISBN))
                .addContainerGap())
        );

        jLabelImage.setAlignmentY(0.0F);
        jLabelImage.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 51, 51), 1, true));
        jLabelImage.setMaximumSize(new java.awt.Dimension(20, 2));
        jLabelImage.setPreferredSize(new java.awt.Dimension(2, 90));

        javax.swing.GroupLayout jPanelBookInfoHeaderWithPicLayout = new javax.swing.GroupLayout(jPanelBookInfoHeaderWithPic);
        jPanelBookInfoHeaderWithPic.setLayout(jPanelBookInfoHeaderWithPicLayout);
        jPanelBookInfoHeaderWithPicLayout.setHorizontalGroup(
            jPanelBookInfoHeaderWithPicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBookInfoHeaderWithPicLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelImage, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelBookHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelBookInfoHeaderWithPicLayout.setVerticalGroup(
            jPanelBookInfoHeaderWithPicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBookInfoHeaderWithPicLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelBookInfoHeaderWithPicLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelImage, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanelBookHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        jTabbedPane.setBackground(new java.awt.Color(51, 153, 255));

        jPanelGoogle.setOpaque(false);
        jPanelGoogle.setLayout(new java.awt.BorderLayout());

        jScrollPane.setAlignmentX(0.0F);
        jScrollPane.setAlignmentY(0.0F);
        jScrollPane.setOpaque(false);

        jTextAreaGoogleDescription.setEditable(false);
        jTextAreaGoogleDescription.setBackground(new java.awt.Color(51, 153, 255));
        jTextAreaGoogleDescription.setColumns(20);
        jTextAreaGoogleDescription.setFont(new java.awt.Font("Trebuchet MS", 1, 13)); // NOI18N
        jTextAreaGoogleDescription.setForeground(new java.awt.Color(153, 255, 153));
        jTextAreaGoogleDescription.setLineWrap(true);
        jTextAreaGoogleDescription.setRows(20);
        jTextAreaGoogleDescription.setAlignmentX(0.0F);
        jTextAreaGoogleDescription.setAlignmentY(0.0F);
        jTextAreaGoogleDescription.setBorder(null);
        jTextAreaGoogleDescription.setOpaque(false);
        jTextAreaGoogleDescription.setPreferredSize(new java.awt.Dimension(100, 16));
        jScrollPane.setViewportView(jTextAreaGoogleDescription);

        jPanelGoogle.add(jScrollPane, java.awt.BorderLayout.CENTER);

        jPanelGoogleRating.setOpaque(false);
        jPanelGoogleRating.setLayout(new java.awt.GridLayout(1, 1));
        jPanelGoogle.add(jPanelGoogleRating, java.awt.BorderLayout.PAGE_START);

        jTabbedPane.addTab("Google Books", jPanelGoogle);

        jPanelUserComments.setBackground(new java.awt.Color(51, 153, 255));
        jPanelUserComments.setAlignmentX(0.0F);
        jPanelUserComments.setAlignmentY(0.0F);
        jPanelUserComments.setOpaque(false);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jTextAreaUserComment.setBackground(new java.awt.Color(230, 244, 255));
        jTextAreaUserComment.setColumns(20);
        jTextAreaUserComment.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTextAreaUserComment.setLineWrap(true);
        jTextAreaUserComment.setRows(5);
        jTextAreaUserComment.setText(USER_COMMENT_INITIAL_VALUE);
        jTextAreaUserComment.setWrapStyleWord(true);
        jTextAreaUserComment.setAlignmentX(0.0F);
        jTextAreaUserComment.setAlignmentY(0.0F);
        jTextAreaUserComment.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jTextAreaUserComment.setPreferredSize(new java.awt.Dimension(204, 20));
        jTextAreaUserComment.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
			public void focusGained(java.awt.event.FocusEvent evt) {
                jTextAreaUserCommentFocusGained(evt);
            }
            @Override
			public void focusLost(java.awt.event.FocusEvent evt) {
                jTextAreaUserCommentFocusLost(evt);
            }
        });
        jScrollPane1.setViewportView(jTextAreaUserComment);

        jPanelUserRater.setOpaque(false);
        jPanelUserRater.setLayout(new java.awt.GridLayout(1, 0));

        jLabelSubmitButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabelSubmitButton.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabelSubmitButton.setAlignmentY(0.0F);
        jLabelSubmitButton.setFocusCycleRoot(true);
        jLabelSubmitButton.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabelSubmitButtonMouseClicked(evt);
            }
        });

        jScrollPaneUsersComments.setBackground(new java.awt.Color(51, 153, 255));
        jScrollPaneUsersComments.setBorder(null);
        jScrollPaneUsersComments.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPaneUsersComments.setAlignmentX(0.0F);
        jScrollPaneUsersComments.setAlignmentY(0.0F);

        jPanelComments.setBackground(new java.awt.Color(51, 153, 255));
        jPanelComments.setLayout(new javax.swing.BoxLayout(jPanelComments, javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPaneUsersComments.setViewportView(jPanelComments);

        javax.swing.GroupLayout jPanelUserCommentsLayout = new javax.swing.GroupLayout(jPanelUserComments);
        jPanelUserComments.setLayout(jPanelUserCommentsLayout);
        jPanelUserCommentsLayout.setHorizontalGroup(
            jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserCommentsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPaneUsersComments)
                    .addGroup(jPanelUserCommentsLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanelUserRater, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelSubmitButton, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanelUserCommentsLayout.setVerticalGroup(
            jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUserCommentsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUserCommentsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelUserCommentsLayout.createSequentialGroup()
                        .addComponent(jPanelUserRater, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelSubmitButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPaneUsersComments, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
        );

        jTabbedPane.addTab("User Comments", jPanelUserComments);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelBookInfoHeaderWithPic, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTabbedPane, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelBookInfoHeaderWithPic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jLabelAddToMyBooksMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelAddToMyBooksMouseClicked
        if (jLabelAddToMyBooks.isEnabled()) {
            addBookToMyList(book);
            jLabelAddToMyBooks.setEnabled(false);
            jLabelIWantIt.setEnabled(false);
        }
    }//GEN-LAST:event_jLabelAddToMyBooksMouseClicked

    private void jLabelEnlargeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelEnlargeMouseClicked
        if (getMaximumSize().height == CLOSE_PANEL_HEIGHT) {
            setMaximumHeight(OPEN_PANEL_HEIGHT);
            jLabelEnlarge.setText("[-]");
            if (myUserRater == null) { // expanding panels aren't initialized yet
                initGoogleDescPanel();
                initUserCommentsPanel();
            }
        } else {
            setMaximumHeight(CLOSE_PANEL_HEIGHT);
            jLabelEnlarge.setText("[+]");
        }

        setVisible(false);
        setVisible(true);
    }//GEN-LAST:event_jLabelEnlargeMouseClicked

    private void jLabelSubmitButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelSubmitButtonMouseClicked
        if (!jLabelSubmitButton.isEnabled()) {
            return;
        }

        if (myUserRater.getRating() == 0) {
            JOptionPane.showMessageDialog(this, "Please vote for the book rating before submitting.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        String textComment = jTextAreaUserComment.getText();
        if (textComment.equals(USER_COMMENT_INITIAL_VALUE)) {
            textComment = "(no contents)";
        }
        BookComment comment = GUIUtils.addBookComment(book, MainForm.getMyUser(), textComment, myUserRater.getRating(), this);
        disableCommentSubmission();

        jPanelComments.add(new JPanelComment(comment), 0);
        usersRater.setRating((int) Math.round(((double) (usersRater.getVoters() * usersRater.getRating() + comment.getRating())) / (usersRater.getVoters() + 1)));
        usersRater.setVoters(usersRater.getVoters() + 1);

    }//GEN-LAST:event_jLabelSubmitButtonMouseClicked

    private void jTextAreaUserCommentFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextAreaUserCommentFocusGained
        GUIUtils.handleJTextFieldFocusGained(jTextAreaUserComment, USER_COMMENT_INITIAL_VALUE);
    }//GEN-LAST:event_jTextAreaUserCommentFocusGained

    private void jTextAreaUserCommentFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextAreaUserCommentFocusLost
        GUIUtils.handleJTextFieldFocusLost(jTextAreaUserComment, USER_COMMENT_INITIAL_VALUE);
    }//GEN-LAST:event_jTextAreaUserCommentFocusLost

    private void jLabelIWantItMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelIWantItMouseClicked
        if (jLabelIWantIt.isEnabled()) {
            MainForm.searchForUsers(book);
        }
    }//GEN-LAST:event_jLabelIWantItMouseClicked

    private void jLabelAddToMyBooksMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabelAddToMyBooksMouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_jLabelAddToMyBooksMouseEntered
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelAddToMyBooks;
    private javax.swing.JLabel jLabelAuthor;
    private javax.swing.JLabel jLabelBookName;
    private javax.swing.JLabel jLabelCategory;
    private javax.swing.JLabel jLabelCategoryPageCount;
    private javax.swing.JLabel jLabelEnlarge;
    private javax.swing.JLabel jLabelISBN;
    private javax.swing.JLabel jLabelIWantIt;
    private javax.swing.JLabel jLabelImage;
    private javax.swing.JLabel jLabelPublicationYear;
    private javax.swing.JLabel jLabelPublisher;
    private javax.swing.JLabel jLabelSubmitButton;
    private javax.swing.JLabel jLabelWiki;
    private javax.swing.JPanel jPanelBookHeader;
    private javax.swing.JPanel jPanelBookInfoHeaderWithPic;
    private javax.swing.JPanel jPanelBookTitle;
    private javax.swing.JPanel jPanelComments;
    private javax.swing.JPanel jPanelGoogle;
    private javax.swing.JPanel jPanelGoogleRating;
    private javax.swing.JPanel jPanelRating;
    private javax.swing.JPanel jPanelUserComments;
    private javax.swing.JPanel jPanelUserRater;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPaneUsersComments;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JTextArea jTextAreaGoogleDescription;
    private javax.swing.JTextArea jTextAreaUserComment;
    // End of variables declaration//GEN-END:variables

    private void initHeaderComponents() {
        jLabelBookName.setText("<html><u>" + book.getName() + "</u></html>");
        //bookPanel(book);
        if (book.getThumbnailURL() != null) {
            GUIUtils.setImageAtSize(jLabelImage, 60, 90, book.getThumbnailURL());
        } else {
            GUIUtils.setImageAtSize(jLabelImage, 60, 90, "/resources/book.png");
        }
        GUIUtils.setJComponentFontAndColor(jLabelBookName, 15);

        initTextHeaderJlabel(jLabelAuthor, listToString(book.getAuthors()));
        initTextHeaderJlabel(jLabelCategory, listToString(book.getCategories()));
        initTextHeaderJlabel(jLabelCategoryPageCount, book.getNumOfPages() + "");
        initTextHeaderJlabel(jLabelISBN, book.getISBN());
        initTextHeaderJlabel(jLabelPublicationYear,
                MySQLFormatDate.getYearString(book.getReleaseYear()));
        initTextHeaderJlabel(jLabelPublisher, listToString(book.getPublishers()));

        GUIUtils.setURLLink(jLabelIWantIt, null, true);
        GUIUtils.setURLLink(jLabelWiki, book.getWikipediaURL(), true);
        GUIUtils.setURLLink(jLabelAddToMyBooks, null, true);
        GUIUtils.setURLLink(jLabelEnlarge, null, false);

        int voters = 0;
        double rating = 0;
        try {
            voters = DBAccessEngine.BookComment().selectCount(book.getID(), new Book(0, ""));
            rating = DBAccessEngine.BookComment().selectAvg(book.getID());
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
        }
        usersRater = new Rater(((int) Math.round(rating)), voters);
        usersRater.setEnabled(false);
        jPanelRating.add(usersRater);
        if (disableIWantIrForBooksIOwn()) {
            return;
        }
    }

    private void initGoogleDescPanel() {
    	    	
    	final JPanelBookInfo thisObj = this;
    	
    	ThreadPool.submitTask(new ThreadCallable<Future<Void>, Void>() {

			@Override
			public Void call() throws Exception {
				
		        if (book.getLanguage() == null && book.getGoogleRating() == 0.0 && book.getDescription() == null) {
		            if (book.getISBN() == null || book.getISBN().equals("") || GUIUtils.setGoogleDetails(book, thisObj) == null) {
		                jTabbedPane.remove(jPanelGoogle);
		                setArgument(null);
		                return null;
		            }
		        }

		        String desc = book.getDescription() + "\nLanguage: " + book.getLanguage();
		        jTextAreaGoogleDescription.setText(desc);

		        Rater rater = new Rater(((int) Math.round(book.getGoogleRating())), -1);
		        rater.setEnabled(false);
		        jPanelGoogleRating.add(rater);
                setArgument(null);
		        return null;
			}
    		
    	});
    	
    }

    private void initUserCommentsPanel() {
        GUIUtils.setImageAtSize(jLabelSubmitButton, 130, 30, "/resources/submit.png");
        GUIUtils.setURLLink(jLabelSubmitButton, null, false);
        myUserRater = new Rater(0, -1);
        jPanelUserRater.add(myUserRater);

        for (BookComment comment : getBookComments(book)) {
            jPanelComments.add(new JPanelComment(comment));
        }
        try {
            if (DBAccessEngine.BookComment().exists(MainForm.getMyUser().getID(), book.getID(), new User(0, ""))) {
                disableCommentSubmission();
            }
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
        }
    }

    @SuppressWarnings("unchecked")
    public List<BookComment> getBookComments(Book book) {
        List<BookComment> comments = null;
        try {
            comments = (List<BookComment>) DBAccessEngine.BookComment().select(book.getID(), new Book(0, ""));
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
        }
        return comments;
    }

    @SuppressWarnings("unchecked")
	private void addBookToMyList(Book book) {
        try {
            ((IDBAccess<User>) DBAccessEngine.User()).insert(MainForm.getMyUser(), book);
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
        }
    }

    private void setMaximumHeight(int height) {
        setMaximumSize(new Dimension(Integer.MAX_VALUE, height));
        setPreferredSize(new Dimension(getWidth(), height));
    }

    private void disableCommentSubmission() {
        jTextAreaUserComment.setText("Your comment is presented below.");
        jTextAreaUserComment.setEnabled(false);
        myUserRater.setEnabled(false);
        jLabelSubmitButton.setEnabled(false);
    }

    @SuppressWarnings("unchecked")
	private boolean disableIWantIrForBooksIOwn() {
        List<Book> books;
        try {
            books = (List<Book>) DBAccessEngine.User().selectAll(MainForm.getMyUser().getID(), new Book(0, ""));
            for (Book b : books) {
                if (b.getID() == book.getID()) {
                    {
                        jLabelIWantIt.setEnabled(false);
                        jLabelAddToMyBooks.setEnabled(false);
                        return true;
                    }
                }
            }
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, this);
        }
        return false;
    }
}
